package com.useeinfo.oa.open.admin;

import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.SelectVO;
import com.useeinfo.framework.sugar.func.lang.StringUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.biz.PositionBiz;
import com.useeinfo.oa.modules.base.entity.ApplicationModule;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.base.entity.Position;
import com.useeinfo.oa.modules.security.biz.SystemUserBiz;
import com.useeinfo.oa.modules.security.entity.SystemUser;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: YiYouFuPlatform
 * @author: Taogq
 * @createDate: 2018-12-12
 * @createTime: 13:28:45
 */
@Controller
@RequestMapping("/web/admin/position")
public class PositionAdminManageAction {

    static Logger logger = LoggerFactory.getLogger(PositionAdminManageAction.class);

    @Autowired
    private PositionBiz positionBiz;
    @Autowired
    private SystemUserBiz systemUserBiz;


    /**
     * 企业岗位--跳转页
     *
     * @return
     */
    @RequestMapping("/list")
    public ModelAndView list() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("pages/admin/position/list");
        return mv;
    }

    /**
     * 企业岗位列表接口
     *
     * @param pageNo
     * @param pageSize
     * @param positionName
     * @return
     */
    @RequestMapping("/listJson")
    @ResponseBody
    public JSONObject listJson(HttpServletRequest request, String pageNo, String pageSize, String positionName,String positionCode) {
        QueryParam queryParam = new QueryParam(pageNo, pageSize);
        Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(request);
        if (null != enterprise) {
            queryParam.getSqlMap().put("enterpriseId", enterprise.getId().toString());
        }
        if (!StringConverters.null2String(positionName).equals("")) {
            queryParam.getSqlMap().put("positionName", positionName);
        }
        if (StringUtils.isNoneBlank(positionCode)) {
            queryParam.getSqlMap().put("positionCode", positionCode);
        }
        return positionBiz.findJSONList(queryParam);
    }

    @RequestMapping("/editPage")
    public ModelAndView editPage(String id) {
        Position position = positionBiz.findModel(StringConverters.null2Long(id));
        ModelAndView mv = new ModelAndView();
        mv.addObject("positon", position);
        mv.setViewName("pages/admin/position/edit");
        return mv;
    }

    @RequestMapping("/addOrUpdate")
    @ResponseBody
    public ResultDto editOrUpdate(HttpServletRequest httpServletRequest, Position position) {
        Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(httpServletRequest);
        position.setEnterprise(enterprise);
        try {
            return positionBiz.saveUpdate(position);
        } catch (Exception e) {
            logger.info(e.getMessage());
            return ResultUtil.error();
        }

    }

    /**
     * 获取岗位-选择框
     */
    @RequestMapping("/getPositionListMagic")
    @ResponseBody
    public List<SelectVO> getPositionListMagic(@RequestParam(value = "q", required = false) String q,
                                               @RequestParam(value = "id", required = false) String id,
                                               @RequestParam(value = "organizationId", required = false) String organizationId,
                                               HttpServletRequest request) {
        Enterprise enterprise=SessionKeyUtils.getSessionEnterprise(request);
        List<SelectVO> selectVOS = new ArrayList<>();
        Position position = positionBiz.findModel(StringConverters.ToLong(id));

        if (position != null) {
            selectVOS.add(new SelectVO(position.getName(), position.getId() + ""));
        }
        if (StringUtils.isEmpty(organizationId)) {
            return selectVOS;
        }
        return positionBiz.getPositionByOrgna(organizationId, enterprise.getId().toString());
    }


    /**
     * 删除岗位
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResultDto delete(String id,HttpServletRequest request) {
        Enterprise enterprise=SessionKeyUtils.getSessionEnterprise(request);
        Position position=positionBiz.findModel(StringConverters.null2Long(id));
        QueryParam queryParam=new QueryParam();
        queryParam.getSqlMap().put("enterpriseId",enterprise.getId()+"");
        queryParam.getSqlMap().put("positionId",id);
        List<SystemUser> systemUserList=systemUserBiz.findList(queryParam);
        if(CollectionUtils.isEmpty(systemUserList)){
            positionBiz.deleteLogic(position);
            return ResultUtil.success();
        }else{
            return ResultUtil.error("该岗位下有人员，不能删除");
        }
    }
}
